/*
 * OnlineSlideshowView.java
 */

package onlineslideshow;

import java.awt.Dimension;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JScrollPane;

/**
 * The application's main frame.
 */
public class OnlineSlideshowView extends FrameView {

    public OnlineSlideshowView(SingleFrameApplication app) {
        super(app);

        initComponents();

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer)(evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = OnlineSlideshowApp.getApplication().getMainFrame();
            aboutBox = new OnlineSlideshowAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        OnlineSlideshowApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        left1 = new javax.swing.JPanel();
        top1 = new javax.swing.JPanel();
        right1 = new javax.swing.JPanel();
        bottom1 = new javax.swing.JPanel();
        contentPanel = new javax.swing.JPanel();
        leftPanel = new javax.swing.JPanel();
        top2 = new javax.swing.JPanel();
        bottom2 = new javax.swing.JPanel();
        previewTab = new javax.swing.JTabbedPane();
        previewScrollPaneFolder = new javax.swing.JScrollPane();
        previewPanelFolder = new javax.swing.JPanel();
        imagePreviewChooser1 = new at.nebbl.beans.ImagePreviewChooser();
        imagePreviewChooser3 = new at.nebbl.beans.ImagePreviewChooser();
        imagePreviewChooser4 = new at.nebbl.beans.ImagePreviewChooser();
        imagePreviewChooser5 = new at.nebbl.beans.ImagePreviewChooser();
        previewScrollPaneFile = new javax.swing.JScrollPane();
        previewPanel1 = new javax.swing.JPanel();
        previewPanelFiles = new javax.swing.JPanel();
        imagePreviewChooser2 = new at.nebbl.beans.ImagePreviewChooser();
        imagePreviewChooser6 = new at.nebbl.beans.ImagePreviewChooser();
        imagePreviewChooser7 = new at.nebbl.beans.ImagePreviewChooser();
        imagePreviewChooser8 = new at.nebbl.beans.ImagePreviewChooser();
        filesControls = new javax.swing.JPanel();
        controlsPanel = new javax.swing.JPanel();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();
        jPanel4 = new javax.swing.JPanel();

        mainPanel.setName("mainPanel"); // NOI18N
        mainPanel.setLayout(new java.awt.BorderLayout());

        left1.setName("left1"); // NOI18N
        left1.setPreferredSize(new java.awt.Dimension(30, 283));

        javax.swing.GroupLayout left1Layout = new javax.swing.GroupLayout(left1);
        left1.setLayout(left1Layout);
        left1Layout.setHorizontalGroup(
            left1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 30, Short.MAX_VALUE)
        );
        left1Layout.setVerticalGroup(
            left1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 363, Short.MAX_VALUE)
        );

        mainPanel.add(left1, java.awt.BorderLayout.LINE_START);

        top1.setName("top1"); // NOI18N
        top1.setPreferredSize(new java.awt.Dimension(661, 20));

        javax.swing.GroupLayout top1Layout = new javax.swing.GroupLayout(top1);
        top1.setLayout(top1Layout);
        top1Layout.setHorizontalGroup(
            top1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 661, Short.MAX_VALUE)
        );
        top1Layout.setVerticalGroup(
            top1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 20, Short.MAX_VALUE)
        );

        mainPanel.add(top1, java.awt.BorderLayout.PAGE_START);

        right1.setName("right1"); // NOI18N
        right1.setPreferredSize(new java.awt.Dimension(30, 363));

        javax.swing.GroupLayout right1Layout = new javax.swing.GroupLayout(right1);
        right1.setLayout(right1Layout);
        right1Layout.setHorizontalGroup(
            right1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 30, Short.MAX_VALUE)
        );
        right1Layout.setVerticalGroup(
            right1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 363, Short.MAX_VALUE)
        );

        mainPanel.add(right1, java.awt.BorderLayout.LINE_END);

        bottom1.setName("bottom1"); // NOI18N
        bottom1.setPreferredSize(new java.awt.Dimension(661, 20));

        javax.swing.GroupLayout bottom1Layout = new javax.swing.GroupLayout(bottom1);
        bottom1.setLayout(bottom1Layout);
        bottom1Layout.setHorizontalGroup(
            bottom1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 661, Short.MAX_VALUE)
        );
        bottom1Layout.setVerticalGroup(
            bottom1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 20, Short.MAX_VALUE)
        );

        mainPanel.add(bottom1, java.awt.BorderLayout.PAGE_END);

        contentPanel.setName("contentPanel"); // NOI18N
        contentPanel.setLayout(new java.awt.BorderLayout());

        leftPanel.setName("leftPanel"); // NOI18N
        leftPanel.setPreferredSize(new java.awt.Dimension(300, 363));
        leftPanel.setLayout(new java.awt.BorderLayout());

        top2.setName("top2"); // NOI18N
        top2.setPreferredSize(new java.awt.Dimension(300, 15));

        javax.swing.GroupLayout top2Layout = new javax.swing.GroupLayout(top2);
        top2.setLayout(top2Layout);
        top2Layout.setHorizontalGroup(
            top2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
        top2Layout.setVerticalGroup(
            top2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 15, Short.MAX_VALUE)
        );

        leftPanel.add(top2, java.awt.BorderLayout.PAGE_START);

        bottom2.setName("bottom2"); // NOI18N
        bottom2.setPreferredSize(new java.awt.Dimension(300, 15));

        javax.swing.GroupLayout bottom2Layout = new javax.swing.GroupLayout(bottom2);
        bottom2.setLayout(bottom2Layout);
        bottom2Layout.setHorizontalGroup(
            bottom2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
        bottom2Layout.setVerticalGroup(
            bottom2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 15, Short.MAX_VALUE)
        );

        leftPanel.add(bottom2, java.awt.BorderLayout.PAGE_END);

        previewTab.setName("previewTab"); // NOI18N
        previewTab.setPreferredSize(new java.awt.Dimension(300, 316));

        previewScrollPaneFolder.setName("previewScrollPaneFolder"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(onlineslideshow.OnlineSlideshowApp.class).getContext().getResourceMap(OnlineSlideshowView.class);
        previewPanelFolder.setToolTipText(resourceMap.getString("previewPanelFolder.toolTipText")); // NOI18N
        previewPanelFolder.setMaximumSize(new java.awt.Dimension(300, 32767));
        previewPanelFolder.setName("previewPanelFolder"); // NOI18N
        previewPanelFolder.setPreferredSize(new java.awt.Dimension(290, 286));

        imagePreviewChooser1.setName("imagePreviewChooser1"); // NOI18N
        imagePreviewChooser1.setPreferredSize(new java.awt.Dimension(100, 110));
        previewPanelFolder.add(imagePreviewChooser1);

        imagePreviewChooser3.setName("imagePreviewChooser3"); // NOI18N
        imagePreviewChooser3.setPreferredSize(new java.awt.Dimension(100, 110));
        previewPanelFolder.add(imagePreviewChooser3);

        imagePreviewChooser4.setName("imagePreviewChooser4"); // NOI18N
        imagePreviewChooser4.setPreferredSize(new java.awt.Dimension(100, 110));
        previewPanelFolder.add(imagePreviewChooser4);

        imagePreviewChooser5.setName("imagePreviewChooser5"); // NOI18N
        imagePreviewChooser5.setPreferredSize(new java.awt.Dimension(100, 110));
        previewPanelFolder.add(imagePreviewChooser5);

        previewScrollPaneFolder.setViewportView(previewPanelFolder);

        previewTab.addTab(resourceMap.getString("previewScrollPaneFolder.TabConstraints.tabTitle"), previewScrollPaneFolder); // NOI18N

        previewScrollPaneFile.setName("previewScrollPaneFile"); // NOI18N

        previewPanel1.setToolTipText(resourceMap.getString("previewPanel1.toolTipText")); // NOI18N
        previewPanel1.setName("previewPanel1"); // NOI18N
        previewPanel1.setLayout(new java.awt.BorderLayout());

        previewPanelFiles.setName("previewPanelFiles"); // NOI18N
        previewPanelFiles.setPreferredSize(new java.awt.Dimension(290, 203));

        imagePreviewChooser2.setName("imagePreviewChooser2"); // NOI18N
        imagePreviewChooser2.setPreferredSize(new java.awt.Dimension(100, 110));
        previewPanelFiles.add(imagePreviewChooser2);

        imagePreviewChooser6.setName("imagePreviewChooser6"); // NOI18N
        imagePreviewChooser6.setPreferredSize(new java.awt.Dimension(100, 110));
        previewPanelFiles.add(imagePreviewChooser6);

        imagePreviewChooser7.setName("imagePreviewChooser7"); // NOI18N
        imagePreviewChooser7.setPreferredSize(new java.awt.Dimension(100, 110));
        previewPanelFiles.add(imagePreviewChooser7);

        imagePreviewChooser8.setName("imagePreviewChooser8"); // NOI18N
        imagePreviewChooser8.setPreferredSize(new java.awt.Dimension(100, 110));
        previewPanelFiles.add(imagePreviewChooser8);

        previewPanel1.add(previewPanelFiles, java.awt.BorderLayout.CENTER);

        filesControls.setBackground(resourceMap.getColor("filesControls.background")); // NOI18N
        filesControls.setName("filesControls"); // NOI18N
        filesControls.setPreferredSize(new java.awt.Dimension(293, 50));

        javax.swing.GroupLayout filesControlsLayout = new javax.swing.GroupLayout(filesControls);
        filesControls.setLayout(filesControlsLayout);
        filesControlsLayout.setHorizontalGroup(
            filesControlsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 293, Short.MAX_VALUE)
        );
        filesControlsLayout.setVerticalGroup(
            filesControlsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 50, Short.MAX_VALUE)
        );

        previewPanel1.add(filesControls, java.awt.BorderLayout.PAGE_END);

        previewScrollPaneFile.setViewportView(previewPanel1);

        previewTab.addTab(resourceMap.getString("previewScrollPaneFile.TabConstraints.tabTitle"), previewScrollPaneFile); // NOI18N

        leftPanel.add(previewTab, java.awt.BorderLayout.CENTER);

        contentPanel.add(leftPanel, java.awt.BorderLayout.LINE_START);

        controlsPanel.setName("controlsPanel"); // NOI18N

        javax.swing.GroupLayout controlsPanelLayout = new javax.swing.GroupLayout(controlsPanel);
        controlsPanel.setLayout(controlsPanelLayout);
        controlsPanelLayout.setHorizontalGroup(
            controlsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 301, Short.MAX_VALUE)
        );
        controlsPanelLayout.setVerticalGroup(
            controlsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 363, Short.MAX_VALUE)
        );

        contentPanel.add(controlsPanel, java.awt.BorderLayout.CENTER);

        mainPanel.add(contentPanel, java.awt.BorderLayout.CENTER);

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(onlineslideshow.OnlineSlideshowApp.class).getContext().getActionMap(OnlineSlideshowView.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 661, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 491, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        jPanel4.setName("jPanel4"); // NOI18N

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel bottom1;
    private javax.swing.JPanel bottom2;
    private javax.swing.JPanel contentPanel;
    private javax.swing.JPanel controlsPanel;
    private javax.swing.JPanel filesControls;
    private at.nebbl.beans.ImagePreviewChooser imagePreviewChooser1;
    private at.nebbl.beans.ImagePreviewChooser imagePreviewChooser2;
    private at.nebbl.beans.ImagePreviewChooser imagePreviewChooser3;
    private at.nebbl.beans.ImagePreviewChooser imagePreviewChooser4;
    private at.nebbl.beans.ImagePreviewChooser imagePreviewChooser5;
    private at.nebbl.beans.ImagePreviewChooser imagePreviewChooser6;
    private at.nebbl.beans.ImagePreviewChooser imagePreviewChooser7;
    private at.nebbl.beans.ImagePreviewChooser imagePreviewChooser8;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel left1;
    private javax.swing.JPanel leftPanel;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JPanel previewPanel1;
    private javax.swing.JPanel previewPanelFiles;
    private javax.swing.JPanel previewPanelFolder;
    private javax.swing.JScrollPane previewScrollPaneFile;
    private javax.swing.JScrollPane previewScrollPaneFolder;
    private javax.swing.JTabbedPane previewTab;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JPanel right1;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JPanel top1;
    private javax.swing.JPanel top2;
    // End of variables declaration//GEN-END:variables

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    private JDialog aboutBox;
}
